package com.itheima.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Category;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @PostMapping("")
    public R<String> save(@RequestBody Category category){

        categoryService.save(category);

       return R.success("新增分类成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("page")
    public R<Page> page(@RequestParam(required = false,defaultValue = "0")Integer page,
                        @RequestParam(required = false,defaultValue = "10")Integer pageSize){
        return R.success(categoryService.getPage(page, pageSize));
    }


    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @PutMapping("")
    public R<String> modify(@RequestBody Category category){

        categoryService.modifyCategory(category);

        return R.success("修改成功");
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("")
    public R<String> delete(String id){


        Boolean res = categoryService.deleteById(id);



        return res?R.success("删除成功"):R.error("当前分类下关联了套餐，不能删除");
    }

    /**
     * 分类下拉列表
     * @return
     */
    @GetMapping("list")
    public R<List<Category>> getAll(@RequestParam(required = false) String type){

        List<Category> categoryList = categoryService.selectAll(type);

        return R.success(categoryList);
    }



}
